using JuMP, GLPK

# 准备一个优化模型
m = Model(with_optimizer(GLPK.Optimizer))
# m = Model(with_optimizer(Gurobi.Optimizer))
# m = Model(with_optimizer(CPLEX.Optimizer))

variable_index = 1:3
constraint_index = 1:3

# 声明变量
@variable(m, x[variable_index] >= 0)

# 目标函数
c = [1; 2; 5]
@objective(m, Max, sum(c[i] * x[i] for i in variable_index))

# 设置约束
A = [-1 1 3; 1 3 -7; 1 0 0]
b = [-5; 10; 10]
# @constraint(m, constant1, sum(A[1, i] * x[i] for i in variable_index) <= b[1])
# @constraint(m, constant2, sum(A[2, i] * x[i] for i in variable_index) <= b[2])
# @constraint(m, constant3, sum(A[3, i] * x[i] for i in variable_index) <= b[3])

# 设置约束方法2
# constraint = Dict()
# for j in constraint_index
#     constraint[j] = @constraint(m, sum(A[j, i] * x[i] for i in variable_index) <= b[j])
# end

# 设置约束方法3
@constraint(m, constraint[j in constraint_index], sum(A[j, i] * x[i] for i in variable_index) <= b[j])

# 输出模型
print(m)

# 求解模型
JuMP.optimize!(m)

# 输出最优解
println("最优解：")
for i in variable_index
    println("x[$i] = ", JuMP.value(x[i]))
end

# 输出对偶问题的解
println("对偶变量：")
for i in 1:2
    println("dual[$i] = ", JuMP.shadow_price(constraint[i]))
end